﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using StockFinder.Model;
using StockFinder.Network;

namespace StockFinder.ExchangeLists.DowJones
{
    public class FinvizDowJonesStockIndexList : IStockIndexList
    {
        const string NASDAQ_SYMBOLS_URL = "http://finviz.com/export.ashx?v=111&f=exch_nasd,idx_dji";
        const string NYSE_SYMBOLS_URL = "http://finviz.com/export.ashx?v=111&f=exch_nyse,idx_dji";
        const string AMEX_SYMBOLS_URL = "http://finviz.com/export.ashx?v=111&f=exch_amex,idx_dji";

        public List<Symbol> GetStockIndexList()
        {
            var stockIndexList = new List<Symbol>();

            //nasdaq
            using (ProxiedWebClient webClient = new ProxiedWebClient())
            {
                string pageSource = webClient.DownloadString(NASDAQ_SYMBOLS_URL);

                var lines = pageSource.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Skip(1);

                foreach (var line in lines)
                {
                    var cells = line.Split(new string[] { "," }, StringSplitOptions.None);

                    var ticker = cells[1].Replace("\"", "");

                    stockIndexList.Add(new Symbol() { Name = ticker, Exchange = Model.Enumerations.Exchange.NASDAQ });
                }
            }

            //nyse
            using (ProxiedWebClient webClient = new ProxiedWebClient())
            {
                string pageSource = webClient.DownloadString(NYSE_SYMBOLS_URL);

                var lines = pageSource.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Skip(1);

                foreach (var line in lines)
                {
                    var cells = line.Split(new string[] { "," }, StringSplitOptions.None);

                    var ticker = cells[1].Replace("\"", "");

                    stockIndexList.Add(new Symbol() { Name = ticker, Exchange = Model.Enumerations.Exchange.NYSE });
                }
            }

            //amex
            using (ProxiedWebClient webClient = new ProxiedWebClient())
            {
                string pageSource = webClient.DownloadString(AMEX_SYMBOLS_URL);

                var lines = pageSource.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Skip(1);

                foreach (var line in lines)
                {
                    var cells = line.Split(new string[] { "," }, StringSplitOptions.None);

                    var ticker = cells[1].Replace("\"", "");

                    stockIndexList.Add(new Symbol() { Name = ticker, Exchange = Model.Enumerations.Exchange.AMEX });
                }
            }

            return stockIndexList;
        }

        public string Name
        {
            get { return "Dow Jones"; }
        }

        public override string ToString()
        {
            return Name;
        }
    }
}
